const BaseService = require("./BaseService.js")
const moment = require('moment')
class ArticleService extends BaseService {
  // 文章列表
  async list(params){
    let model = this.Db('article').where('is_delete','=',0);
    // 研究方向
    if(params.research_direction){
      model = model.where('research_direction','like',`%${params.research_direction}%`)
    }
    // 数据类型
    if(params.data_type){
      model = model.where('data_type','like',`%${params.data_type}%`)
    }
    // 影响因子
    if(params.factor){
      model = model.where('factor','=',params.factor)
    }
    // 公司名称
    if(params.company){
      model = model.where('company','=',params.company)
    }
    // 疾病类型
    if(params.disease_class){
      model = model.where('disease_class','=',params.disease_class)
    }
    // 文章名称
    let result = await model.page(params.current,params.size).select()
    // 总条数
    let totalCount = await model.count()
		return {
      list: result,
      totalCount
    }
  }

  // 文章类型列表
  type_list(params){
    let result = this.Db('article_type').select()
    return result
  }

  // 使用数据类型列表
  data_type_list(params){
    let result = this.Db('data_type').select()
    return result
  }

  // 疾病子类列表
  disease_type_list(params){
    let result = this.Db('disease_type').select()
    return result
  }

  // 详情
  detail(params){
    let result = this.Db('article')
    .field('id,contributor,company,article_name,mid,pid,periodical,factor,publish_time,is_gongyi,is_tongxun,cooperation1,cooperation2,article_direction,article_type,research_direction,data_type,data_class,disease_class,data_volume')
    .where('id','=',params.id).find();
    return result
  }

  // 新增
  create(params){
    this.Db('article').insert({
      ...params,
      is_gongyi: params.is_gongyi == '是' ? 1 : 3,
      is_tongxun: params.is_tongxun == '是' ? 1 : 3,
      create_time: moment().format('YYYY-MM-DD HH:mm:ss'),
      update_time: moment().format('YYYY-MM-DD HH:mm:ss')
    });
    return true
  }

  // 导入数据
  createAll(array){
    // 过滤一下数据
    array = array.map(v=>{
      return {
        contributor: v.contributor,
        company: v.company,
        article_name: v.article_name,
        mid: v.mid,
        pid: v.pid,
        periodical: v.periodical,
        factor: v.factor,
        publish_time: v.publish_time,
        is_gongyi: v.is_gongyi == '是' ? 1 : 3,
        is_tongxun: v.is_tongxun == '是' ? 1 : 3,
        cooperation1: v.cooperation1,
        cooperation2: v.cooperation2,
        article_direction: v.article_direction,
        article_type: v.article_type,
        research_direction: v.research_direction,
        data_type: v.data_type,
        data_class: v.data_class,
        disease_class: v.disease_class,
        data_volume: v.data_volume,
        create_time: moment().format('YYYY-MM-DD HH:mm:ss'),
        update_time: moment().format('YYYY-MM-DD HH:mm:ss')
      }
    })
    this.Db('article').insertAll(array);
    return true
  }

  // 编辑内容
  edit(params){
    this.Db('article').where('id','=',params.id).update({
      ...params,
      update_time: moment().format('YYYY-MM-DD HH:mm:ss')
    });
    return true
  }

  // 删除
  deleteItem(params){
    this.Db('article').where('id','=',params.id).update({is_delete: 1})
    return true
  }
}

module.exports = ArticleService;